//
//  EasyTableView.m
//  PhotobrowerTool
//
//  Created by tianhongyin on 14-4-8.
//  Copyright (c) 2014年 tianhongyin. All rights reserved.
//

#import "EasyTableView.h"

@interface EasyTableView()



@end

@implementation EasyTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.frame = frame;
    }
    return self;
}

- (id)init
{
    self = [super init];
    if(self){
        NSAssert(false, @"please use initWithFrame: method to init view");
    }
    return self;
}

- (void)setupSubViews
{
    [self createTableView];
//    [self create];
}

- (void)createTableView
{
    CGRect newRect = CGRectZero;
    if(!CGRectEqualToRect(self.frame, CGRectZero))
        newRect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.height, self.bounds.size.width);
    
    if(!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:newRect style:_tableStyle];
        _tableView.showsHorizontalScrollIndicator = _showsHorizontalScrollIndicator;
        _tableView.showsVerticalScrollIndicator = _showsVerticalScrollIndicator;
        _tableView.contentInset=UIEdgeInsetsMake(11.f, 0, 11.f, 0);
        _tableView.delegate = self;
        _tableView.scrollsToTop = NO;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.pagingEnabled = _pageEnable;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.contentOffset=CGPointMake(0, 11);
        //把tableview顺时针旋转90度
        CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI/2);
        [_tableView setTransform:transform];
        //旋转之后的table的起始位置发生偏移，在重置一下起始坐标
        _tableView.frame = CGRectMake(0, 0, _tableView.frame.size.width, _tableView.frame.size.height);
//        NSLog(@"---%f---%f---%f---%f",_tableView.frame.origin.x,_tableView.frame.origin.y,_tableView.frame.size.width,_tableView.frame.size.height);
    }
    
    if(!_tableView.superview)
        [self addSubview:_tableView];
}

- (void)reloadData
{
    [_tableView reloadData];
   // [_tableView scrollsToTop];
}


#pragma mark -
#pragma mark UITableView dataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:numberOfRowsInSection:)])
        numberOfRows = [_delegate easyTableView:self numberOfRowsInSection:section];
    
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:cellForRowAtIndexPath:)])
        cell = [_delegate easyTableView:self cellForRowAtIndexPath:indexPath];
    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger number = 0;
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableViewNumberOfSectionsInTableView:)])
        number = [_delegate easyTableViewNumberOfSectionsInTableView:self];
    if(number == 0)  //如果实现了这个方法，number至少等于1
        number = 1;
    
    return number;
}

#pragma mark -
#pragma mark UITableView dataSource methods

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:willDisplayCell:forRowAtIndexPath:)])
        [_delegate easyTableView:self willDisplayCell:cell forRowAtIndexPath:indexPath];
}

- (void)tableView:(EasyTableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:willDisplayHeaderView:forSection:)])
        [_delegate easyTableView:self willDisplayHeaderView:view forSection:section];
}

- (void)tableView:(EasyTableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)
{
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:willDisplayFooterView:forSection:)])
        [_delegate easyTableView:self willDisplayFooterView:view forSection:section];
}

- (CGFloat)tableView:(EasyTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 0.;
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:heightForRowAtIndexPath:)])
         height = [_delegate easyTableView:tableView heightForRowAtIndexPath:indexPath];
    return height;
}

- (CGFloat)tableView:(EasyTableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    float height = 0.;
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:heightForHeaderInSection:)])
       height = [_delegate easyTableView:self numberOfRowsInSection:section];
    return height;
}

- (CGFloat)tableView:(EasyTableView *)tableView heightForFooterInSection:(NSInteger)section
{
    float height = 0.;
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:heightForFooterInSection:)])
       height = [_delegate easyTableView:self numberOfRowsInSection:section];
    return height;
}

- (UIView *)tableView:(EasyTableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = nil;
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:viewForHeaderInSection:)])
       view = [_delegate easyTableView:self viewForHeaderInSection:section];
    view.transform = CGAffineTransformMakeRotation(M_PI/2);
    return view;
}

- (UIView *)tableView:(EasyTableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = nil;
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:viewForFooterInSection:)])
        view = [_delegate easyTableView:self viewForFooterInSection:section];
    view.transform = CGAffineTransformMakeRotation(M_PI/2);
    return view;
}

- (void)tableView:(EasyTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(_delegate && [_delegate respondsToSelector:@selector(easyTableView:didSelectRowAtIndexPath:)])
        [_delegate easyTableView:self didSelectRowAtIndexPath:indexPath];
}

#pragma mark - 
#pragma mark cell reuse method

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
{
    id obj = [_tableView dequeueReusableCellWithIdentifier:identifier];
    return obj;
}

@end
